﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entidades.Catalogo;
using Control.Catalogo;
using Entidades.Consultas.Personalizadas;
using System.Web.UI.HtmlControls;

namespace SICOIA.Marcos
{
    public partial class control : System.Web.UI.Page
    {
        EntUsuario entUsuario = null;
        public string pantallaInicial = "";
        public string pantallaBandeja = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["__EVENTARGUMENT"] == "logout")
                DoLogout();
            else
            {
                entUsuario = (EntUsuario)Session["UserPemex"];
                if ((EntUsuario)Session["UserPemex"] == null)
                    Response.Write("<script>parent.window.open('Marcos/EndPEMEX.aspx','_top')</script>");
                else
                {
                    if (entUsuario.CveRol == 11)
                    {
                        this.pantallaInicial = "ProxyUserControls.aspx?Url=BandejaSolicitante";
                        this.pantallaBandeja = this.pantallaInicial;

                        ((HtmlGenericControl)this.Page.FindControl("liMenuSolicituides")).Visible = false;
                        ((HtmlGenericControl)this.Page.FindControl("liMenuAfectadosParticulares")).Visible = false;
                        ((HtmlGenericControl)this.Page.FindControl("liMenuAfectadosFederales")).Visible = false;
                        ((HtmlGenericControl)this.Page.FindControl("liMenuPredios")).Visible = false;
                        
                    }
                    else if (entUsuario.CveRol == 10)
                    {
                        this.pantallaInicial = "ProxyUserControls.aspx?Url=BandejaSupervisores";
                        this.pantallaBandeja = "ProxyUserControls.aspx?Url=Bandeja";
                    }
                    else
                    {
                        this.pantallaInicial = "ProxyUserControls.aspx?Url=Bandeja";
                        this.pantallaBandeja = this.pantallaInicial;

                        if (entUsuario.CveRol == 3 || entUsuario.CveRol == 2)// Gestor o cordinador
                        {
                            ((HtmlGenericControl)this.Page.FindControl("liMenuSolicituides")).Visible = false;
                            ((HtmlGenericControl)this.Page.FindControl("liMenuAfectadosParticulares")).Visible = false;
                            ((HtmlGenericControl)this.Page.FindControl("liMenuAfectadosFederales")).Visible = false;
                            ((HtmlGenericControl)this.Page.FindControl("liMenuPredios")).Visible = false;
                            ((HtmlGenericControl)this.Page.FindControl("liMenuObra")).Visible = false;
                            
                        }
                    }

                    if (!Page.IsPostBack)
                    {
                        lblUsuario.Text = entUsuario.NombreCompleto;
                        lblRol.Text = entUsuario.Rol;
                        ObrasPendientes();
                    }
                }
            }
        }

        private void DoLogout()
        {
            Session["UserPemex"] = null;
            Session.Abandon();
            Response.Cache.SetNoServerCaching();
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetNoStore();
            Response.Cache.SetExpires(new DateTime(1900, 1, 1, 0, 0, 0, 0));

            Response.Write("<SCRIPT LANGUAGE=javascript>");
            Response.Write("{");
            Response.Write(" var Backlen=history.length;");
            Response.Write(" history.go(-Backlen);");
            Response.Write(" window.location.replace('./Login.aspx?Parameter=logout'); ");
            Response.Write(" window.location.href='../Login.aspx?Parameter=logout'; ");
            Response.Write("}");
            Response.Write("</SCRIPT>");
        }

        protected void btnSalir_Click(object sender, ImageClickEventArgs e)
        {
            Session.Remove("UserPemex");
            Session.Abandon();
            Response.Redirect("../Login.aspx", true);  
        }

        private void ObrasPendientes()
        {
            try
            {
                CtrlSolicitudTrabajosRealizar ctrl = new CtrlSolicitudTrabajosRealizar();
                List<EntObraConDetalle> lista =
                ctrl.CriteriosObtenerTodosBandeja(" and A.CVE_ESTATUS <> 4 ", "", entUsuario);

                lblTotalPendientes.Text = lista.Count<EntObraConDetalle>().ToString();
            }
            catch (Exception ex)
            {
                string a = ex.Message;
            }
        }
    }
}
